package com.example.kalapp;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

public class CalcActivity extends Activity {
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calc);
		
		final Button calcbtn = (Button) findViewById(R.id.calcBtn);
		final Button foodsearchbtn = (Button) findViewById(R.id.BtnFoodsearch);
		final AutoCompleteTextView foodText  = (AutoCompleteTextView) findViewById(R.id.food);
		final TextView result  = (TextView) findViewById(R.id.result);
		final TextView calPrGr  = (TextView) findViewById(R.id.calPrGr);
		final TextView editGram  = (TextView) findViewById(R.id.edit_gram);
		final DatabaseHandler db = new DatabaseHandler(this);
		
		final List<Food> foods = db.getAllFoods();
		final List<String> names = db.getAllNames();
		
		
		foodText.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, names));
		foodText.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
            {
            Log.d("your selected item",""+names.get(position));
            //s1.get(position) is name selected from autocompletetextview
            // now you can show the value on textview. 
    }

        });
		
		calcbtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				double kalories  = 0;
				double antalGram = 0;
        		
        		try {
        			kalories = Double.parseDouble(calPrGr.getText().toString());
        		} catch(NumberFormatException nfe) {
        		   System.out.println("Could not parse " + nfe);
        		} 
        		try {
        			antalGram = Double.parseDouble(editGram.getText().toString());
        		} catch(NumberFormatException nfe1) {
        		   System.out.println("Could not parse " + nfe1);
        		} 
        		
        		double kalPrgram = kalories/100;
        		double kalIAlt = kalPrgram*antalGram;
        		String resu = String.valueOf(kalIAlt);
        		result.setText(resu);
			}
        });
		
		foodsearchbtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String madvare = foodText.getText().toString();
				//String madvare = String.valueOf(food.getText());
				double kalprhund = 0;
				for (Food food : foods)
				    if (food.getName().equals(madvare)){
				    	kalprhund = food.getKalorier();
				    }
				//for (int i, i< )
				/*
				for (Food food : foods) {
					String currName = food.getName();
					do 
					{
						kalprhund = food.getKalorier();
					}
					while (currName == madvare);
					
			      }
				*/
        		String resul = String.valueOf(kalprhund);
        		calPrGr.setText(resul);
			}
        });
		
		
	}

	public void toCalc (View view){
    	//double outputValue = 0;
    			
    	Intent intent = new Intent(this, MainActivity.class);
    	//EditText editText = (EditText) findViewById(R.id.edit_gram);
    	//Hent sql f�devare "klasse" her istedet for 100
    	//String kalorier = editText.getText().toString();
    	//intent.putExtra(EXTRA_MESSAGE, kalorier);
    	startActivity(intent);
    }
	
	/*
	@Override
	public void afterTextChanged(Editable arg0) {
	 // TODO Auto-generated method stub

	}
	
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
	  int after) {
	 // TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	 // TODO Auto-generated method stub

	}
	*/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.calc, menu);
		return true;
	}
	
}
